<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="50%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> 复制图纸 </span>
    </template>
    <div style="color: red;margin-left: 20px;">提示:请复制EXCEL文件至输入框中。图名/图号/材料/数量/单价</div>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsM8OrderOrderForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { orderPicReadCopyData } from '/@/api/m8/order/order';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('setting.pricemodel.wecharPriceModelInv');
  const { showMessage } = useMessage();
  const record = ref<WecharPriceModelUse>({} as WecharPriceModelUse);
  const getTitle = computed(() => ({
    icon: 'icon-user',
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: '',
      field: 'selIds',
      component: 'InputTextArea',
      required: true,
      // componentProps: {
      //   placeholder: '请先复制图纸',
      //   rows: 32,
      //   allowClear: true,
      // },
      componentProps: ({ tableAction, formModel }) => {
        return {
          placeholder: '请先复制图纸',
          maxlength: -1,
          rows: 32,
          onblur: (e) => {
            let value = e.target.value;
            let newValue = value.replace(/\t/g, '{@@}');
            formModel['selIds'] = newValue;
          }
        }
      },
      required: true,
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    // const res = await wecharPriceModelUseForm(data);
    // record.value = (res.wecharPriceModelUse || {}) as WecharPriceModelUse;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {

    const data = await validate();
    console.log(data);
    
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const res = await orderPicReadCopyData(data);
      // showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', res);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
